﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using CartoGraphic.Model2;

namespace CartoGraphic.Model
{
    class DataFrame
    {
        public double left, down, up, right, scale;  //视口 

        //public double left_t, down_t, up_t, right_t;    //变换视口

        public int screenWidth, screenHeight;

        public List<BaseDrawingObj> geos;

        public Graphics g;

        public void trans()
        {          
            //double scaleX = 1.0*screenWidth / (right - left);
            //double scaleY = 1.0 * screenHeight / (up - down);
            //this.scale = Math.Min(scaleX, scaleY);
            //int moveX = (int)((screenWidth-scale * (right - left))*0.5);
            //int moveY = (int)((screenHeight - scale * (up - down)) * 0.5);
            //foreach (AbstractGeometry geo in geos)
            //{
            //    if (geo.dimention == 0)
            //    {
            //        PointGeo point = (PointGeo)geo;
            //        point.centerPoint.sX = (int)(scale * (point.centerPoint.X - left))+moveX;
            //        point.centerPoint.sY = screenHeight-(int)(scale * (point.centerPoint.Y - down))+moveY;
            //    }
            //    else if (geo.dimention == 1)
            //    {
            //        PolyLineGeo line = (PolyLineGeo)geo;
            //        List<MyPoint> points = line.points;
            //        foreach (MyPoint point in points)
            //        {
            //            point.sX = (int)(scale * (point.X - left)) + moveX;
            //            point.sY = screenHeight - (int)(scale * (point.Y - down))+moveY;
            //        }
            //    }
            //    else if (geo.dimention == 2)
            //    {
            //        PolygonGeo polygon = (PolygonGeo)geo;
            //        List<MyPoint> points = polygon.points;
            //        foreach (MyPoint point in points)
            //        {
            //            point.sX = (int)(scale * (point.X - left)) + moveX;
            //            point.sY = screenHeight - (int)(scale * (point.Y - down))+moveY;
            //        }
            //    }
            //}
        }

        public Point transPoint(double x, double y)
        {
            double scaleX = 1.0 * screenWidth / (right - left);
            double scaleY = 1.0 * screenHeight / (up - down);
            this.scale = Math.Min(scaleX, scaleY);
            int moveX = (int)((screenWidth - scale * (right - left)) * 0.5);
            int moveY = (int)((screenHeight - scale * (up - down)) * 0.5);
            return new Point((int)(scale * (x - left)) + moveX, screenHeight - (int)(scale * (y - down)) + moveY);
        }

        public DataFrame(double left, double down, double up, double right, double scale)
        {
            this.left = left;
            this.down = down;
            this.up = up;
            this.right = right;
            this.scale = scale;
        }

        public DataFrame()
        {
            StreamReader sr = new StreamReader(@"Points.csv", Encoding.GetEncoding("gb2312"));
            string sFile = sr.ReadToEnd();
            string[] vals = sFile.Split('\n');
            List<double> x = new List<double>();
            List<double> y = new List<double>();
            for (int i = 0; i < vals.Length; i++)
            {
                string sLine = vals[i];
                string[] sVal = sLine.Split(',');
                if (sVal.Length < 2)
                    continue;
                x.Add(Double.Parse(sVal[0]));
                y.Add(Double.Parse(sVal[1]));
            }
            this.left = x.Min();
            this.right = x.Max();
            this.down = y.Min();
            this.up = y.Max();
        }
    }
}
